<?php
class UserAction extends Action{
	//构造函数
	public function _initialize(){
		if(!isset($_SESSION["pid"])){
			if(!isset($_COOKIE["pid"])){
				$this->redirect("partner/Public/login");
			}else{
				$_SESSION["pid"] = $_COOKIE["pid"];
				$_SESSION["name"] = M("Partner")->where("pid = ".$_SESSION["pid"])->find()["name"];
			}
		}
		
		$this->assign("mod","user");
	}
	public function index(){
		$pid = $_SESSION["pid"];
		$partner = M("Partner")->where("pid = $pid")->find();
		$this->assign("partner",$partner);
		$this->display("user_index");
	}
	
	
	
	
	public function edit(){
		if(isset($_REQUEST["type"])){
			$this->assign("type",$_REQUEST["type"]);
		}
		$pid = $_SESSION["pid"];
		$partner = M("Partner")->where("pid = $pid")->find();
		$this->assign("partner",$partner);
		$this->display("user_edit");
	}
	public function editOK(){
		$pid = $_POST["pid"];
		$password = $_POST["password"];
		$pa = M("Partner")->where("pid = $pid")->find()["password"];
		//验证密码
		if($pa == md5($password)){		
			//密码正确
			$partner = M("Partner");
			$partner->name = $_POST["name"];
			$partner->contact = $_POST["contact"];
			$partner->phone = $_POST["phone"];
			$partner->qq = $_POST["qq"];
			if($partner->where("pid = $pid")->save()){
				$data=array("massage"=>"修改成功","url"=>"partner/User/index");
			}else{
				$data=array("massage"=>"修改失败","url"=>"partner/User/index");
			}
			$this->redirect("partner/Public/redirectHtml",$data);
		}else{
			//不正确
			$data=array("massage"=>"密码错误","url"=>"partner/User/edit");
			$this->redirect("partner/Public/redirectHtml",$data);	
		}
		
		//修改信息
	}
	
	public function changePassword(){
		$pid = $_REQUEST["pid"];
		$oldpassword = $_REQUEST["oldpassword"];
		$password = $_REQUEST["password"];
		$confirmpassword = $_REQUEST["confirmpassword"];
		if($password != $confirmpassword){
			$data=array("massage"=>"密码不一致","url"=>"partner/User/edit","type"=>"changepw");
			$this->redirect("partner/Public/redirectHtml",$data);
			exit;
		}
		//验证密码
		$pa = M("Partner")->where("pid = $pid")->find()["password"];
		if($pa == md5($oldpassword)){
			//修改信息
			$partner = M("Partner");
			$partner->password = md5($password);
			if($partner->save()){
				$data=array("massage"=>"修改成功","url"=>"partner/User/index");
				$this->redirect("partner/Public/redirectHtml",$data);
			}else{
				$data=array("massage"=>"修改失败","url"=>"partner/User/index");
				$this->redirect("partner/Public/redirectHtml",$data);
			}
		}else{
			$data=array("massage"=>"密码错误","url"=>"partner/User/edit","type"=>"changepw");
			$this->redirect("partner/Public/redirectHtml",$data);
			exit;
		}
		
	}

}